package heap

// Object 既可以表示普通的对象，也可以表示数组
type Object struct {
	class *Class
	data  interface{}
	extra interface{} //记录Object结构体实例的额外信息
}

// NewObject 新建对象
func newObject(class *Class) *Object {
	return &Object{class: class, data: newSlots(class.instanceSlotCount)}
}

//region getters

func (e *Object) Fields() Slots {
	return e.data.(Slots)
}

func (e *Object) Class() *Class {
	return e.class
}

func (e *Object) Extra() interface{} {
	return e.extra
}

func (e *Object) SetExtra(extra interface{}) {
	e.extra = extra
}

func (e *Object) IsInstanceOf(class *Class) bool {
	return class.IsAssignableFrom(e.class)
}

//endregion

func (e *Object) GetRefVar(name, descriptor string) *Object {
	field := e.class.getField(name, descriptor, false)
	slots := e.data.(Slots)
	return slots.GetRef(field.slotId)
}

func (e *Object) SetRefVar(name, descriptor string, ref *Object) {
	field := e.class.getField(name, descriptor, false)
	slots := e.data.(Slots)
	slots.SetRef(field.slotId, ref)
}
